package cn.yunyichina.provider.pay.service.impl;

import cn.yunyichina.provider.pay.dao.PaymentSettingDao;
import cn.yunyichina.provider.pay.entity.PaymentSetting;
import cn.yunyichina.provider.pay.entity.SettlementSetting;
import cn.yunyichina.provider.pay.exception.PaySettingNotFoundException;
import cn.yunyichina.provider.pay.service.PaymentSettingService;
import com.alibaba.dubbo.common.utils.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author zhangkewen
 */
@Service
public class PaymentSettingServiceImpl implements PaymentSettingService {

    private static final Logger logger = LoggerFactory.getLogger(PaymentSettingServiceImpl.class);

    private RedisCacheServiceImpl redisCacheService;

    private final PaymentSettingDao paymentSettingDao;

    @Autowired
    public PaymentSettingServiceImpl(RedisCacheServiceImpl redisCacheService, PaymentSettingDao paymentSettingDao) {
        this.redisCacheService = redisCacheService;
        this.paymentSettingDao = paymentSettingDao;
    }

    @Override
    public PaymentSetting queryPaymentSettingByAppId(String appId) throws PaySettingNotFoundException {
        return redisCacheService.getPaymentSettingByAppId(appId);
    }

    public PaymentSetting getPaymentSettingByParentId(String parentId) throws PaySettingNotFoundException {
        PaymentSetting wheres = new PaymentSetting();
        wheres.setId(parentId);

        List<PaymentSetting> paymentSettings = paymentSettingDao.select(wheres);
        if (CollectionUtils.isEmpty(paymentSettings)) {
            logger.info("[PaymentSettingAppRelationServiceImpl][queryPaymentSettingByAppId]->查询ParentId:" + parentId + "在表[PAY_PAYMENT_SETTING]中没有记录!");
            throw new PaySettingNotFoundException("查询ParentId:" + parentId + "在表[PAY_PAYMENT_SETTING]中没有记录!");
        }

        return paymentSettings.get(0);
    }

    public PaymentSetting getSubPaymentSettingByParentIdAndOrgCodeAndPayMode(String parentId, String orgCode, int payMode) throws PaySettingNotFoundException {
        PaymentSetting wheres = new PaymentSetting();
        wheres.setParentId(parentId);
        wheres.setOrgCode(orgCode);
        wheres.setPayMode((short) payMode);
        wheres.setIsSubPay((short) 1);

        List<PaymentSetting> paymentSettings = paymentSettingDao.select(wheres);
        if (CollectionUtils.isEmpty(paymentSettings)) {
            logger.info("[PaymentSettingAppRelationServiceImpl][queryPaymentSettingByParentId]->查询ParentId:" + parentId + "在表[PAY_PAYMENT_SETTING]中没有记录!");
            throw new PaySettingNotFoundException("查询ParentId:" + parentId + "在表[PAY_PAYMENT_SETTING]中没有记录!");
        }

        return paymentSettings.get(0);
    }

    @Override
    public PaymentSetting queryPaymentSettingByAppcodeAndOrgcodeAndPaymode(String appCode, String orgCode, short payMode) throws PaySettingNotFoundException {
        logger.info("PaymentSettingServiceImpl.queryPaymentSettingByAppcodeAndOrgcodeAndPaymode.params -> {}, {}, {}", appCode, orgCode, payMode);

        String payOrgCode = null;
        //1.取出结算链条的支付节点
        if (!StringUtils.isEmpty(orgCode)) {
            SettlementSetting settlementSetting = redisCacheService.getSettlementSetting(appCode, orgCode, payMode);
            if (settlementSetting != null) {
                payOrgCode = settlementSetting.getPayOrgCode();
                if (!StringUtils.isEmpty(payOrgCode) && payOrgCode.equals(settlementSetting.getPlatformCode())) {
                    payOrgCode = null;
                }
            }
        }

        //2.取出配置的paymentSettingId
        String paymentSetttingId = redisCacheService.getPaymentSettingAppRelation(appCode, payOrgCode, payMode).getPaymentSettingId();

        //3.取出最终的配置
        return redisCacheService.getPaymentSettingById(paymentSetttingId);
    }

}
